package com.deng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deng.common.lang.Result;
import com.deng.entity.User;
import com.deng.mapper.UserMapper;
import com.deng.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deng.shiro.AccountProfile;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dengwenxiong
 * @since 2021-05-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Result register(User user) {
        int count = this.count(new QueryWrapper<User>()
                .eq("email", user.getEmail())
                .or()
                .eq("username", user.getUsername())
        );
        if(count>0) return Result.fail("用户名或邮箱已经被占用");
        User temp=new User();
        temp.setUsername(user.getUsername());
        temp.setAvatar("/res/images/avatar/default.png");
        temp.setPassword(SecureUtil.md5(user.getPassword()));
        temp.setEmail(user.getEmail());
        temp.setCreated(new Date());
        temp.setPoint(0);
        temp.setVipLevel(0);
        temp.setCommentCount(0);
        temp.setPostCount(0);
        temp.setGender("0");
        //存入数据库
        this.save(temp);
        return Result.success();

    }

    @Override
    public AccountProfile login(String email, String password) {
        User user=this.getOne(new QueryWrapper<User>().eq("email",email));
        if(user==null){
            throw new UnknownAccountException();
        }
        if(!user.getPassword().equals(password)){
            throw new IncorrectCredentialsException();
        }
        user.setLasted(new Date());
        this.updateById(user);
        AccountProfile profile=new AccountProfile();
        //BeanUtil.copyProperties(a,b);将a中存在的属性赋值给b，b中存在的属性A中一定要有
        BeanUtil.copyProperties(user,profile);
        return profile;
    }
}
